// pages/my/sport/index.js
const myaudio = wx.createInnerAudioContext();
var App = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        totaltime: '',
        sport: '',
        all: 0,
        runStart: "开始",
        speed: 0,
        second: 0,
        precent: 0,
        times: 0,
		timm:0,
        idd: 0,
        subid: 0,
		allTime:0,
		addTime:0,
		api:App.api,
		timenodes:0,
		runTime:0
    },
    looper: '',
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
		//myaudio.src = 'http://localhost:8080/bb/images/runSchedule/78fc770c76134e4793f0271ce9ebb2df.mp3'
        var that = this;
        var idd = options.idd
        var subid = options.subid;
        var totaltime = options.totaltime;
		var allTime = options.totaltime;
		var runTime = options.runTime;
		var timenodes = JSON.parse(options.timenodes);
        var prea = (100 / totaltime).toFixed(2);
		var speed = prea * runTime; 
        console.log(prea);
        that.setData({
			totaltime: totaltime,
			runTime: runTime,
            times: prea,
            idd: idd,
            subid: subid,
			allTime: allTime,
			speed:speed,
			timm: totaltime - runTime,
			addTime: totaltime - runTime,
			timenodes: timenodes
        })
        that.getSport(subid);
    },
    returns: function() {
        wx.navigateBack({
            delta: 1
        })
		myaudio.pause();
    },
    getSport: function(subid) {
        var that = this;
        App.ajax.ajax('schdule.do?method=selectJieduans', {
            JSESSIONID: wx.getStorageSync("JSESSIONID"),
            i_bb_run_schedule_sub_id: subid
        }, function(res) {
            console.log(res.data.result);
            //     var all = res.sport.total;
            for (var i = 0; i < res.data.result.length; i++) {
				res.data.result[i].second = res.data.result[i].time_length;
                res.data.result[i].time_length = ((res.data.result[i].time_length / that.data.totaltime) * 100).toFixed(2);
            }
            that.setData({
                precent: res.data.result
            })
        })
    },
    runStart: function() {
        var that = this;
        var runStart = that.data.runStart;
        var speed = that.data.speed;
        var totaltime = that.data.totaltime;
		var loadTime = that.data.allTime - that.data.timm;
		console.log(loadTime)
		console.log(that.data.allTime)
        var times = that.data.times;
        var interval = function() {
			var loadTime = that.data.allTime - that.data.timm;
			console.log(loadTime)
			for(var i = 0; i < that.data.timenodes.length; i++) {
			
				if (that.data.timenodes[i].timeNode == loadTime) {
					console.log(2)
					 myaudio.src = that.data.api + that.data.timenodes[i].music;
					 myaudio.play();
				}
			}
            if (that.data.speed > 100) {
				that.saveData('结束', loadTime + 1);
                that.setData({
                    runStart: "开始"
                })
                clearInterval(that.looper);
                return false
			} 

            if (totaltime == 0) {
                that.setData({
                    totaltime: 0
                })
                clearInterval(that.looper);
                return false
            } else {
                that.setData({
                    speed: parseFloat(that.data.times) + parseFloat(that.data.speed),
					timm: that.data.timm - 1,
					addTime: that.data.addTime + 1
                })
            }
        }
        if (runStart == "开始") {
            that.setData({
                runStart: "暂停"
            })
			that.saveData('开始', loadTime);
            that.looper = setInterval(interval, 1000)

        } else {
            that.setData({
                runStart: "开始"
            })
			that.saveData('暂停',loadTime);
			myaudio.pause();
            clearInterval(this.looper);
            console.log(this.looper)
        }
    },
    saveData: function(state, time) {
		var that = this;
        App.ajax.ajax('schdule.do?method=saveJindu', {
            JSESSIONID: wx.getStorageSync('JSESSIONID'),
            i_bb_user_schedule_id: that.data.idd,
            i_bb_run_schedule_sub_id: that.data.subid,
			status: state,
            time_length: time
        }, function(res) {
            console.log(res)
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})